﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System.Collections.Generic;
using System.Net;
using Ciatel.ISO8583.Amex;
using Ciatel.ISO8583.Amex.Utilities;
using ESF.General;
using ESF.SSO.Core.Entities;
using System;
using ESF.SSO.Core.Entities.Log;
using ESF.SSO.Core.Entities.Exceptions;
using ESF.General.Entity;
using ESF.SSO.Core.Entities.BasicReferences;
using System.ServiceModel;
using System.Net.Sockets;

#endregion


namespace ESF.SSO.ISO8583.ServerRequestHandler.Operations
{
	internal static class GetList_I
	{
		internal static void ProcessRequest (List<Message> pRequests, IPEndPoint pSenderIPEndPoint, IPEndPoint pForwarderIPEndPoint, out List<Message> pResponses, out List<object> pProcessedObjects)
		{
			Message request;
			Message response;
			
			CustomField customField_57;
			
			List<BasicReference> list;
			
			string tmpSessionID;
			
			int listIndex;
			int index;
			int parteIndex;
			
			
			request = pRequests [0];
			
			request.ApplicationData.DataElements.CheckHasMandatoryFields
			(
				TheKnownFields.CustomPrivateField_57
			);
			
			
			customField_57 = request.ApplicationData.DataElements.GetCustomPrivateField_57 ();
			
			
			try
			{
				using (SSOServiceClient.SSOServiceClient ssoClient = new SSOServiceClient.SSOServiceClient ())
				{
					list = ssoClient.ListBasicReference (
						new BasicReferenceType () { ID = (int) customField_57.GetNumericFieldAsLong (4) }); // N 4
				}
				
				pResponses = new List<Message> ();
				
				
				#region Mensaje 1
				
				response = request.GenerateBasicResponse (pForwarderIPEndPoint.Port);

				customField_57 = new CustomField ();
				
				tmpSessionID = Guid.NewGuid ().ToString ();
				
				// TODO: Poner el SessionID
				customField_57.AddFieldWithVariableLength(tmpSessionID, 40);	// Ans .. 40
				
				if (list == null || list.Count == 0)
				{
					customField_57.AddField (0, 4);		// N 4
				}
				else
				{
					customField_57.AddField(list.Count, 4);		// N 4
				}
				
				response.ApplicationData.DataElements.SetCustomPrivateField_57 (customField_57);
				
				pResponses.Add (response);
				
				#endregion
				
				#region Mensaje 2 .. n
				
				if (list.Count > 0)
				{
					// Indice de pagina arranca en 1
					for (listIndex = 0, index = 1; listIndex < list.Count; index ++)
					{
						response = request.GenerateBasicResponse (pForwarderIPEndPoint.Port);
						
						customField_57 = new CustomField ();

						customField_57.AddFieldWithVariableLength(tmpSessionID, 40);	// Ans .. 40
						customField_57.AddField (index, 4);			// N 4
						
						for (parteIndex = 0; parteIndex < 12 && listIndex < list.Count; parteIndex ++, listIndex ++)
						{
							customField_57.AddFieldWithVariableLength (list [listIndex].Code, 60);	// Ans .. 60
							customField_57.AddFieldWithVariableLength (list [listIndex].Name, 60);	// Ans .. 60
						}
						
						response.ApplicationData.DataElements.SetCustomPrivateField_57 (customField_57);
						
						pResponses.Add (response);
					}
				}
				
				#endregion
					
				pProcessedObjects = null;
			}
			catch (FaultException gEx)
			{
				throw new GenericException (SSOExceptionCodes.ToSSOISO8583ExceptionCode (gEx.Code.Name), gEx.Message);
			}			
		}
	}
}
